<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache,no-store,must-revalidate">
    <meta http-equiv="Cache" content="no-cache">
		<title>新增页面</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form" action="" lay-filter="form-filter">
			<div class="mainBox">
				<div class="main-container">
					
					<div class="layui-form-item">
						<label class="layui-form-label">异常名称</label>
						<div class="layui-input-block">
							<input type="text" name="abnormalItemName" lay-verify="required" autocomplete="off" placeholder="请输入作业项目"
								class="layui-input">
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label" style="width:85px">异常内容清单</label>
						<div class="layui-form-mid layui-word-aux">不同选项使用分号(<span style="color:red;font-size:110%">;</span> )分隔</div>
						<div class="layui-input-block">
							<textarea name="abnormalItemContent" placeholder="请输入选择清单" lay-verify="required" class="layui-textarea"></textarea>
						</div>
					</div>

					<div class="layui-form-item">
						<label class="layui-form-label">作业属于</label>
						<div class="layui-input-block">
							<input type="radio" name="belong2" value="belong2SecD" title="工程下所有设备" lay-filter="belong2-filter" checked>
							<input type="radio" name="belong2" value="belong2MacNo" title="设备号" lay-filter="belong2-filter" >
						</div>
					</div>
					
					<!-- 所属工程,填充 -->
					<div class="layui-form-item">
						<div id="belong2SecD">

						</div>
					</div>
					<!-- 所属设备,填充 -->
					<div class="layui-form-item" id="layui-form-item-belong2MacNo">
						<div id="belong2MacNo">

						</div>
					</div>
					<!-- 处置方法,填充 -->
					<div class="layui-form-item">
						<label class="layui-form-label">处置方法</label>
						<div class="layui-input-block">
							<select name="abHandleList" lay-verify="required" xm-select="abHandleList" xm-select-skin="normal" xm-select-search	>
								<!-- <option value="1">北京</option>
								<option value="2" selected="selected">上海</option>
								<option value="3">广州</option>
								<option value="4" selected="selected">深圳</option>
								<option value="5">天津</option> -->
							</select>
						</div>
					</div>
				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="abnormalItemEdit">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<!-- <button type="reset" class="pear-btn pear-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button> -->
				</div>
			</div>
		</form>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>

		<script type="text/javascript">
			// 加载自定义模块,检查浏览器和token
			layui.extend({
        myheader: "{/}/lay-ext/myheader",
      });
			
			
			layui.use(['form', 'jquery','myheader','layer','laytpl','select'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				let myheader = layui.myheader;
				let layer = layui.layer;
				let laytpl = layui.laytpl;
				let select = layui.select;

				var optionData={
					secdlist:[],
					macnolist:[]
				}
        // 定义全局 ajax的请求头
        myheader.ajaxGlobal(myheader.checkToken().tokenValue);

				// 获取父页的数据
				optionData.fuData = layui.sessionData("fu2zi").fuData;
				
				// 清空父传子时临时用的session对象
        layui.sessionData("fu2zi", null);

				// 初始化
				initPage();

				// 注册作业属于的单选事件
				belong2Event()
				
				// 注册所属工程-下拉列表的选择事件
				belong2SecDSelectEvent()

				initEdit()

				// form 确认上传
				form.on('submit(abnormalItemEdit)', function(data) {
					$.ajax({
						url: parent.layui.myheader.baseUrl()+ '/abnormalItem/uptAbnormalItem',
						data:JSON.stringify({
							abnormalItemID: optionData.fuData.abnormalItem.abnormalItemID,
    					abnormalItemName:data.field.abnormalItemName,
    					abnormalItemContent:data.field.abnormalItemContent.replaceAll(/[;；]/g,';'),
							abHandleList:data.field.abHandleList.replaceAll(/[,，]/g,';'),
    					secDeptID:data.field.belong2SecD,
  						belong2MacNo:data.field.belong2MacNo===undefined?"":data.field.belong2MacNo
						}),
						contentType: 'application/json',		// 请求时的数据类型
						type: 'post',
						success: function(res) {
							if (res.data==1) {
								layer.msg("已保存", {
									time: 1000
								}, function() {
									parent.layer.close(parent.layer.getFrameIndex(window
										.name)); //关闭当前页
									parent.layui.table.reloadData('abnormalItem-table',{
										scrollPos: 'fixed'  // 保持滚动条位置不变 - v2.7.3 新增
									});
								});
							} else {
								layer.msg("保存失败", {
									time: 1000
								});
							}
						}
					})
					return false;
				});

				// 从父页获取的数据,渲染到子页上
				function initEdit(){
					//1. 直接赋值
					form.val("form-filter",{
							"abnormalItemName": optionData.fuData.abnormalItem.abnormalItemName,
							"abnormalItemContent": optionData.fuData.abnormalItem.abnormalItemContent
					})
					//2.渲染处置方法选中项
					renderAbHandleTplWithSelected(getAbHandleList(optionData.fuData.abnormalItem.secDeptID));

					//4.作业所属渲染
					if(optionData.fuData.abnormalItem.belong2MacNo==null || optionData.fuData.abnormalItem.belong2MacNo===""){
						form.val("form-filter",{
							"belong2" : "belong2SecD",
							"belong2SecD": optionData.fuData.abnormalItem.secDeptID
							
						})
					} else {
						getAllMacNo(optionData.fuData.abnormalItem.secDeptID)
						form.val("form-filter",{
							"belong2" : "belong2MacNo",
							"belong2SecD": optionData.fuData.abnormalItem.secDeptID,
							"belong2MacNo":optionData.fuData.abnormalItem.belong2MacNo
						})
					}

				}

				function initPage(){
					// 获取全部工程List
					getAllSecD();
					$("#belong2MacNo").html('')
				}

				// 获取 工程 的 form 模板
				function getAllSecD(){
					// 获取select 数据
					$.ajax(parent.layui.myheader.baseUrl()+"/secDept/getAllSecDeptName",{
						async:false, // 同步方法
						success: function(res){
							if(res.data.length>0){
								optionData.secdlist =[...res.data]
							} else {
								optionData.secdlist =[]
								parent.layer.msg('未获取有效数据')
							}
						}
					})
					
					// 填充layui模板
					let getTpl = optionTpl.innerHTML
					laytpl(getTpl).render(optionData.secdlist,function(html){
						$("#belong2SecD").html(html)
						// form重新渲染select
						form.render('select');
					})
				}

				function belong2Event(){
					// 单选切换事件
					form.on('radio(belong2-filter)',function(data){
						if(data.value =="belong2SecD"){
							$("#belong2MacNo").html('')
							
						} else if (data.value=="belong2MacNo"){
							laytpl(optionMacTpl.innerHTML).render({},function(html){
								$("#belong2MacNo").html(html)
								// form重新渲染select
								form.render('select');
							})
						}
						select.data('abHandleList','local',{
							arr: ""
						})
						//return 
						//$("[name=belong2MacNo] option:selected").text("")
						// 清空 select machineNo 模板
						//clearMacSelectTpl()
						
						// 获取并渲染secd
						getAllSecD();
					})
				}

				function belong2SecDSelectEvent(){
					// 所属工程选择框, 选择事件
					form.on('select(belong2SecD-filter)',function(data){
						// 选中即渲染处置方法list
						// 获取异常处置清单,并赋值给formSelect
						renderAbHandleTplWithSelected(getAbHandleList(data.value));
						if((form.val('form-filter').belong2)==="belong2MacNo"){
							getAllMacNo(data.value);
						}
						return;

						// 单选选中 设备号时, ajax获取设备号list,并填充模板
						//if($("input[name='belong2']:checked").val()==="belong2MacNo"){
						//	getAllMacNo(data.value);
						//}
					})
				}

				// 获取和填充设备号 select 设备号
				function getAllMacNo(secDeptID){
					$.ajax(parent.layui.myheader.baseUrl()+"/machine/getMachineList4SecDept",{
						async:false,  //同步方法
						data: {
							secDeptID: secDeptID
						},
						success: function(res){
							if(res.data.length>0){
								optionData.macnolist =[...res.data]
							} else {
								optionData.macnolist=[]
								parent.layer.msg('未获取有效数据')
							}
						}
					})
					// 填充layui模板
					let getTpl = optionMacTpl.innerHTML
					laytpl(getTpl).render(optionData.macnolist,function(html){
						$("#belong2MacNo").html(html)
						
						//$("[name='belong2MacNo']").html(html)
						// form重新渲染select
						form.render('select');
					})
				}
				// 清空select 设备号模板
				function clearMacSelectTpl(){
					// 填充layui模板
					let getTpl = optionMacTpl.innerHTML
					laytpl(getTpl).render([],function(html){
						$("[name='belong2MacNo']").html(html)
						// form重新渲染select
						form.render('select');
					})
				}
				
				// 获取异常处置清单
				function getAbHandleList(secDeptID){
					let list;
					$.ajax({
						async : false,
						url: parent.layui.myheader.baseUrl()+ '/abHandleItem/getAllOfSecDept',
						data:{secDeptID:secDeptID},
						type: 'get',
						success : function(res){
							if(res.data.length>0) {
								list=[...res.data]
							}else {
								parent.layer.msg('未获取有效数据')
							}
						}
					})

					if(typeof(list)=="undefined") return;
					for(let i=0;i<list.length;i++){
						delete list[i].abHandleItemContentType
						delete list[i].enabled
						delete list[i].secDeptID
					}

					return JSON.stringify(list).replace(/abHandleItemName/g,"name").replace(/abHandleItemID/g,"value");
				}
				
				// 渲染异常处置模板(自动渲染选中)
				function renderAbHandleTplWithSelected(listjson){
					if(typeof(listjson)=="undefined") return;

					let jsonObj = JSON.parse(listjson);
					for(let i=0;i<jsonObj.length;i++){
						if(optionData.fuData.abnormalItem.abHandleList.indexOf(jsonObj[i].value)>-1){
							jsonObj[i].selected="selected"
						}
					}
					select.data('abHandleList','local',{
						arr: jsonObj
					})
				}
			
			})
		</script>
		<script id="optionTpl" type="text/html">
			<label class="layui-form-label">所属工程</label>
			<div class="layui-input-block">
				<select name="belong2SecD" lay-verify="required" lay-filter="belong2SecD-filter">
					<option value=""></option>
					{{# layui.each(d,function(index,item){ }}
					<option value="{{item.secDeptID}}">{{item.secDeptName}} </option>
					{{# }); }}		
				</select>
			</div>
		</script>
		
		<script id="optionMacTpl" type="text/html">
			<label class="layui-form-label">所属设备</label>
			<div class="layui-input-block">
				<select name="belong2MacNo" lay-verify="required" lay-filter="belong2MacNo-filter">
					<option value=""></option>
					{{# layui.each(d,function(index,item){ }}
					<option value="{{item.machineNo}}">{{item.machineNo}} </option>
					{{# }); }}
				</select>
			</div>
		</script>

	</body>
</html>
